<?php
	namespace app\admin\controller;

	use \think\Db;

	use \think\Session;

	class strategy extends Base{
		function show(){
			try{
				if($_POST){
					$page=(int)input('post.page');

					if(!$page){$page=1;}

					$length=5;

					$start=($page-1)*$length;

					$count=Db::name('strategy')->count('s_id');

					$list=Db::name('strategy')->limit($start,$length)->select();

					if($list){
						foreach($list as $k=>$v){
							$list[$k]['s_text']=htmlspecialchars_decode($v['s_text']);
						}

						return $this->success(['page'=>$page,'length'=>$length,'count'=>$count,'list'=>$list]);
					}

					return $this->fail('数据为空');
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
		function show_row(){
			try{
				if($_POST){
					$id=(int)input('post.id');

					if(!$id){
						return $this->fail('攻略Id不存在,请重试');
					}

					$row=Db::name('strategy')->where('s_id',$id)->find();

					if(!$row){
						return $this->fail('攻略信息不存在,请重试');
					}

					$row['s_text']=htmlspecialchars_decode($row['s_text']);

					return $this->success(['row'=>$row]);
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
		function upda_btn(){
			try{
				if($_POST){
					$id=(int)input('post.s_id');

					$name=input('post.s_name');

					$title=input('post.s_title');

					$icon=input('post.s_icon');

					$text=htmlspecialchars(input('post.s_text'));

					if(!$name){
						return $this->fail('请输入攻略名称');
					}
					if(!$icon){
						return $this->fail('请上传攻略图片');
					}
					if(!$title){
						return $this->fail('请输入攻略标题');
					}

					$row=Db::name('strategy')->where('s_id',$id)->find();

					if($row){

						if($icon!=$row['s_icon']){
						$icon=str_replace('//'.$_SERVER['HTTP_HOST'],'',$icon);

						$new_icon=str_replace('temp',"upload",$icon);

						if(!is_file('.'.$new_icon) && is_file('.'.$icon)){
							if(copy('.'.$icon,'.'.$new_icon)){
								unlink('.'.$icon);

								$icon=$new_icon;
							}
						}

							$icon='//'.$_SERVER['HTTP_HOST'].$icon;
						}

						$res=Db::name('strategy')->where('s_id',$id)->update(['s_name'=>$name,'s_icon'=>$icon,'s_title'=>$title,'s_text'=>$text]);
						
					}else{

						$dir=ROOTPATH.'/uploads';

						if(!is_dir($dir)){
							mkdir($dir);
						}

						$dir.='/'.date('Ymd');

						if(!is_dir($dir)){
							mkdir($dir);
						}

						$icon=str_replace('//'.$_SERVER['HTTP_HOST'],'',$icon);

						$new_icon=str_replace('temp',"upload",$icon);

						if(copy('.'.$icon,'.'.$new_icon)){
							unlink('.'.$icon);

							$icon=$new_icon;
						}

						$icon='//'.$_SERVER['HTTP_HOST'].$icon;

						$primary=Db::name('primary')->where(['type'=>'strategy'])->find();

						if($primary){

							$id=$primary['id']+1;

							$res=Db::name('primary')->where(['type'=>'strategy'])->setInc('id',1);

						}else{

							$id=1;

							$res=Db::name('primary')->insert(['type'=>'strategy','id'=>1]);

						}

						$userInfo=Session::get('admin_info');

						$time=time();

						$res2=Db::name('strategy')->insert(['s_id'=>$id,'s_name'=>$name,'s_icon'=>$icon,'s_title'=>$title,'s_text'=>$text,'s_userId'=>$userInfo['id'],'s_userName'=>$userInfo['username'],'s_createTime'=>$time]);

					}

					
					return $this->success();
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
	}
?>